<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { systemApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import Pagination from "@/components/Pagination/index.vue";
  import configForm from './configForm.vue';
  import { ElMessageBox } from 'element-plus';
  const configFormRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const listQuery = reactive({
    page:1,
    limit:20,
    key: "",
    category: ""
  })
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const handleForm = (row:any = {})=>{
    configFormRef.value.openDialog(row)
  }
  const handleDel = (row:any = {})=>{
    const { key='', id } = row
    ElMessageBox.confirm(`确定删除key：${key}吗？`, '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      const loading = Toastloading()
      systemApi.delSystems(id).then(()=>{
        ToastSuccess('删除成功')
        getList()
        loading.close()
      }).catch(()=>{
        loading.close()
      })
    }).catch(() => { })
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await systemApi.getSystemsList(listQuery)
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">键名：</div>
          <el-input v-model="listQuery.key" clearable placeholder="请输入键名" class="xm-width200"/>
        </div>
        <div class="labelItem">
          <div class="label">分类：</div>
          <el-select v-model="listQuery.category">
            <el-option label="全部" value=""></el-option>
            <el-option label="基础" value="基础"></el-option>
            <el-option label="支付" value="支付"></el-option>
            <el-option label="风控" value="风控"></el-option>
            <el-option label="其他" value="其他"></el-option>
          </el-select>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="handleForm">新增</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="90" prop="id" label="配置ID" />
      <el-table-column min-width="100" prop="key" label="键" />
      <el-table-column min-width="100" prop="value" label="值" />
      <el-table-column min-width="100" prop="desc" label="描述" />
      <el-table-column width="160" label="操作">
        <template #default="scoped">
          <el-button type="primary" @click="handleForm(scoped.row)">编辑</el-button>
          <el-button type="danger" @click="handleDel(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <configForm ref="configFormRef" @update-list="getList" />
  </div>
</template>
